<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/28 0028
 * Time: 15:42
 */

namespace app\api\controller;
use app\common\lib\Aes;
use app\common\lib\exception\ApiException;
use app\common\lib\IAuth;
use app\common\lib\Time;
use think\Controller;

class Common extends Controller
{
    /**
     * header头
     * @var string
     */
    public $headers = '';
    public $page = 1;
    public $size = 10;
    public $from = 0;
    public function _initialize()
    {
        $this->checkPequestAuth();
        $this->testAes();
    }

    /**
     * 检查每次app请求的数据是否合法
     */
    public function checkPequestAuth(){
//        首页需要获取headers的数据
        $headers = request()->header();
//
        if(empty($headers['sign'])){
            throw  new ApiException('sign不存在',400);
        }
        if(!in_array($headers['app_type'],config('app.apptypes'))){
            throw new ApiException('app_type类型不合法',400);
        }
////        IAuth::checkSignPass($headers);exit;
        if (!IAuth::checkSignPass($headers)){
            throw new ApiException('授权码sign失败', 401);
        }
        $this->headers = $headers;
    }


    public function testAes(){
        $data = [
            'did' => '12345dg',
            'version' => 1,
            'time' => Time::get13TimeStamp(),
        ];
        $str = 'col9j6cqegAKiiey3IrXWuwuMgioK3/5b6OUjKathNYIVnKDb7Rin03dOqY2qLWP';
        //YaffLWIjFGQBdNDKwh6iAU3shMoQkeCISiuCx8WMJacLcz++Pw2ZeQvpBUeOQ7II
//        echo IAuth::setSign($data);exit;
//       echo (new Aes())->decrypt($str);exit;
    }
    /**
     * 获取处理的新闻的内容数据
     * @param array $news
     * @return array
     */
    protected  function getDealNews($news = []) {
        if(empty($news)) {
            return [];
        }
        $cats = config('cat.lists');
        foreach($news as $key => $new) {
            $news[$key]['catname'] = $cats[$new['catid']] ? $cats[$new['catid']] : '-';
        }
        return $news;
    }

    /**
     * 获取分页page size 内容
     * @param $data
     */
    public function getPageAndSize($data) {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
        $this->from = ($this->page - 1) * $this->size;
    }

}